package com.woorigil.social.parser;

import java.io.IOException;
import java.io.Reader;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.ValidityException;

import org.springframework.beans.factory.annotation.Value;

import com.woorigil.social.domain.ProviderCode;
import com.woorigil.social.domain.deal.Deal;
import com.woorigil.social.domain.deal.DealListDocument;
import com.woorigil.social.domain.deal.ManageStatus;

/**
 * http://www.wemakeprice.com/wemakeplace/rss/wemakeprice_basic.xml
 */
public class Wemakeprice3XmlParser extends XmlParser {
	@Value("${wemakeprice.deal.path}") 
	String dealPath;

	@Override
	public String getCharsetName() {
		return "UTF-8";
	}

	@Override
	public ProviderCode getProviderCode() {
		return ProviderCode.WM;
	}

	@Override
	public String getSourcePath() {
		return dealPath;
	}

	@Override
	public DealListDocument parse(Reader reader) throws ValidityException, ParsingException, IOException {
		Builder builder = new Builder();
		Document doc = builder.build(reader);
		Element root = doc.getRootElement();
		
		DealListDocument socialDealList = new DealListDocument();
		socialDealList.setSourceProvider("위메이크프라이스");
		socialDealList.setVersion("1.0");
		socialDealList.setHomepage("http://www.wemakeprice.com");
		socialDealList.setLogo("");		
		
		Elements dealsEles = root.getChildElements();
		for (int i = 0; i < dealsEles.size(); i++) {
			Element dealEl = dealsEles.get(i);

			Deal deal = new Deal();
			deal.setProviderCode(ProviderCode.WM);
			deal.setMetaId(generateMetaId(getValue(dealEl.getFirstChildElement("url"))));
			deal.setWebLink(getValue(dealEl.getFirstChildElement("url")));
			deal.setMobileLink(getValue(dealEl.getFirstChildElement("url")));
			deal.setCategory(getValue(dealEl.getFirstChildElement("division")));
			deal.setSection1(getValue(dealEl.getFirstChildElement("region")));
			deal.setTitle(getValue(dealEl.getFirstChildElement("name")));
			deal.setOriginalPrice(Integer.parseInt(getValue(dealEl.getFirstChildElement("price"))));
			deal.setSalesPrice(Integer.parseInt(getValue(dealEl.getFirstChildElement("saleprice"))));
			deal.setDiscountRate(Integer.parseInt(getValue(dealEl.getFirstChildElement("salerate"))));
			deal.setMaxCount(Integer.parseInt(getValue(dealEl.getFirstChildElement("maxcnt"))));
			deal.setMinCount(Integer.parseInt(getValue(dealEl.getFirstChildElement("mincnt"))));
			deal.setNowCount(Integer.parseInt(getValue(dealEl.getFirstChildElement("salecnt"))));
//			deal.setSaleEndAt(DateUtils.toDate(getValue(dealEl.getFirstChildElement("limitdate"))));
			deal.setSalesStatus(getValue(dealEl.getFirstChildElement("today_use")));
			deal.setImageUrl1(getValue(dealEl.getFirstChildElement("image1")));
			deal.setImageUrl2(getValue(dealEl.getFirstChildElement("image2")));
			deal.setImageUrl3(getValue(dealEl.getFirstChildElement("image3")));
			deal.setImageUrl4(getValue(dealEl.getFirstChildElement("image4")));
			deal.setImageUrl5(getValue(dealEl.getFirstChildElement("image5")));
			
			deal.setShopAddress(getValue(dealEl.getFirstChildElement("address")));
			deal.setShopLatitude(getValue(dealEl.getFirstChildElement("lat")));
			deal.setShopLongitude(getValue(dealEl.getFirstChildElement("lng")));

			deal.setImageUrl(deal.getImageUrl1());
			deal.setManageStatus(ManageStatus.S);
			deal.setCategoryId(findCategoryId(getProviderCode(), deal));
			socialDealList.add(deal);
		}
		return socialDealList;
	}

	public String generateMetaId(String value) {
		int idx1 = value.indexOf("daoneday/");
//		int idx2 = value.indexOf("daoneday/");
		return value.substring(idx1 + 9, idx1 + 14);
	}
}
